angular.module('UCX.Cmis')
  .directive('fileDropzone', (DnDService,CmisService,Utils,$translate) ->
    require: '^?form'
    restrict: 'A'
    scope: {
      file: '='
      fileName: '='
      dropzoneHoverClass: '@'
    }
    link: (scope, element, attrs, form) ->
      if CmisService.filePickerModal == 'saveAs' || !CmisService.docOwner || CmisService.isSharedWithMe
        scope.notSupportDropFile=true
      getDataTransfer = (event) ->
        dataTransfer = event.dataTransfer || event.originalEvent.dataTransfer

      # function to prevent default behavior (browser loading image)
      processDragOverOrEnter = (event) ->
        if DnDService.objectIds != null
          return
        if event
          event.preventDefault()  if event.preventDefault
          if scope.notSupportDropFile
            return
          element.addClass scope.dropzoneHoverClass
          return false if event.stopPropagation
        getDataTransfer(event).effectAllowed = 'copy'
        false

      # for dragover and dragenter (IE) we stop the browser from handling the
      # event and specify copy as the allowable effect
      element.bind 'dragover', processDragOverOrEnter
      element.bind 'dragenter', processDragOverOrEnter
      element.bind 'dragleave', ->
        element.removeClass scope.dropzoneHoverClass

      # on drop events we stop browser and read the dropped file via the FileReader
      # the resulting droped file is bound to the image property of the scope of this directive
      element.bind 'drop', (event) ->
        console.log('====Utils.currentFolder====')
        console.log(Utils.currentFolder);
        console.log(Utils.currentFolder_readonly)
        event?.preventDefault()
        if scope.notSupportDropFile
          return
        if Utils.currentFolder.isReadonly()
          Utils.warning($translate.instant('can_not_upload_readonly'))
          $('#file-list').removeClass('active');
          return false
        element.removeClass scope.dropzoneHoverClass
        file = getDataTransfer(event).files
        scope.$emit 'file-dropzone-drop-event', file
        false
  )

